package com.guigu.tms.work.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.tms.commons.entity.aggregation.CoreOrg;
import com.guigu.tms.commons.entity.oms.Order;
import com.guigu.tms.commons.entity.work.TaskPickupDispatch;
import com.guigu.tms.work.mapper.CoreOrgMapper;
import com.guigu.tms.work.mapper.OrderMapper;
import com.guigu.tms.work.mapper.TaskPickupDispatchMapper;
import com.guigu.tms.work.service.IAuthUserService;
import com.guigu.tms.work.service.ITaskPickupDispatchService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 取件、派件任务信息表 服务实现类
 * </p>
 *
 * @author zwj
 * @since 2022-09-27
 */
@Service
public class TaskPickupDispatchServiceImpl extends ServiceImpl<TaskPickupDispatchMapper, TaskPickupDispatch> implements ITaskPickupDispatchService {
    @Autowired
    TaskPickupDispatchMapper taskPickupDispatchMapper;
    @Autowired
    CoreOrgMapper coreOrgMapper;
    @Autowired
    IAuthUserService authUserService;
    @Autowired
    OrderMapper orderMapper;
    @Override
    public Order GetTaskPickupDispatchAndPutInOrder(Order order) {
        //判断订单状态是否属于未取件或网点自寄
        if(order.getStatus()==1||order.getStatus()==2){

        }else{
            //判断是否网点自寄
            if(order.getPickupType()!=1){
                //通过订单id和类型查询出取件信息
                QueryWrapper<TaskPickupDispatch> queryWrapper=new QueryWrapper<>();
                queryWrapper.eq("order_id",order.getOrderId());
                queryWrapper.eq("task_type",1);
                TaskPickupDispatch pickup=taskPickupDispatchMapper.selectOne(queryWrapper);
                //判断网点id是否为空 不为空则通过id查询网点对象 放进pickup对象中
                if(pickup.getAgencyId()!=null&&!pickup.getAgencyId().equals("")){
                    CoreOrg coreOrg = coreOrgMapper.selectById(pickup.getAgencyId());
                    pickup.setAgency(coreOrg);
                }
                //查询快递员信息
                order.setPickUp(authUserService.GetAuthUserAndPutInTaskPickupDispatch(pickup));
            }
        }
        if(order.getStatus()==7||order.getStatus()==8||order.getStatus()==9){
            QueryWrapper<TaskPickupDispatch> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("order_id",order.getOrderId());
            queryWrapper.eq("task_type",2);
            TaskPickupDispatch dispatch=taskPickupDispatchMapper.selectOne(queryWrapper);
            if(dispatch.getAgencyId()!=null&&!dispatch.getAgencyId().equals("")){
                CoreOrg coreOrg = coreOrgMapper.selectById(dispatch.getAgencyId());
                dispatch.setAgency(coreOrg);
            }
            order.setDispatch(authUserService.GetAuthUserAndPutInTaskPickupDispatch(dispatch));
        }
        return order;
    }

    @Override
    public List<TaskPickupDispatch> selectByCourierId(String courierId) {
        QueryWrapper<TaskPickupDispatch> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("courier_id",courierId);
        List<TaskPickupDispatch> taskPickupDispatches = taskPickupDispatchMapper.selectList(queryWrapper);
        for (TaskPickupDispatch task:
             taskPickupDispatches) {
            QueryWrapper<Order> queryWrapper1=new QueryWrapper<>();
            queryWrapper1.eq("order_id",task.getOrderId());
            Order order = orderMapper.selectOne(queryWrapper1);
            task.setOrder(order);
        }
        System.out.println(taskPickupDispatches);
        return taskPickupDispatches;
    }


    @Override
    public void PickupCompleted(TaskPickupDispatch taskPickupDispatch) {
        taskPickupDispatchMapper.updateById(taskPickupDispatch);
        orderMapper.updateById(taskPickupDispatch.getOrder());
    }

}
